package com.zhouchao.web;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhouchao.bean.DormManager;
import com.zhouchao.bean.Info;
import com.zhouchao.dao.DormBuildDao;
import com.zhouchao.dao.InfoDao;
import com.zhouchao.util.DbUtil;
import com.zhouchao.util.TimeUtil;

public class BlankServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		Object currentUserType = session.getAttribute("currentUserType");
		String action = request.getParameter("action");
		InfoDao infodao =new InfoDao();
		DbUtil dbUtil = new DbUtil();
		TimeUtil time = new TimeUtil();
		String tm = time.Time();
		Connection con = null;
		try {
			con=dbUtil.getCon();
			List<Info> infolist =infodao.infoList(con);
			request.setAttribute("infolist", infolist);
			
			
			if("admin".equals((String)currentUserType)) {
				
				request.setAttribute("mainPage", "admin/blank.jsp");
				request.getRequestDispatcher("mainAdmin.jsp").forward(request, response);
				
			} else if("dormManager".equals((String)currentUserType)) {
				
				if("all".equals(action)){
					
					
					
					request.setAttribute("mainPage", "dormManager/blankall.jsp");
					request.getRequestDispatcher("mainManager.jsp").forward(request, response);
	
				}else{
					//发布信息
					DormManager manager = (DormManager)(session.getAttribute("currentUser"));
					int buildId = manager.getDormBuildId();
					String name=manager.getName();
					con=dbUtil.getCon();
					String buildName = DormBuildDao.dormBuildName(con, buildId);
					request.setAttribute("buildName", buildName);
					request.setAttribute("tm", tm);
					request.setAttribute("name", name);
				request.setAttribute("mainPage", "dormManager/blank.jsp");
				request.getRequestDispatcher("mainManager.jsp").forward(request, response);
				}
			} else if("student".equals((String)currentUserType)) {
				
				if("stuall".equals(action)){
					request.setAttribute("mainPage", "student/blankall.jsp");
					request.getRequestDispatcher("mainStudent.jsp").forward(request, response);
	
				}else{
				request.setAttribute("mainPage", "student/blank.jsp");
				request.getRequestDispatcher("mainStudent.jsp").forward(request, response);
				}
				
			}
			
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		
	}

	
}
